package admin

import (
	"laohuo/dao"
	"laohuo/model"
	"net/http"

	"github.com/gin-gonic/gin"
)

type WarehouseBill struct{}

func (c *WarehouseBill) List(ctx *gin.Context) {
	params := struct {
		Page     uint
		Pagesize uint
		Skuid    uint
	}{}
	if err := ctx.ShouldBindQuery(&params); err != nil {
		ctx.String(http.StatusBadRequest, err.Error())
		return
	}
	if params.Page == 0 {
		params.Page = 1
	}
	if params.Pagesize == 0 || params.Pagesize > 100 {
		params.Pagesize = 20
	}

	whb, err := dao.FetchWarehouseBillWithSku(params.Page, params.Pagesize)
	if err != nil {
		ctx.String(http.StatusInternalServerError, err.Error())
	} else {
		total, _ := dao.CountWarehouseBill()
		ctx.JSON(http.StatusOK, &struct {
			Total int
			Data  []*model.WarehousebillDetail
		}{total, whb})
	}
}
